package com.nchu.controller;
import com.nchu.mappers.EvaluationMapper;
import com.nchu.pojo.*;
import com.nchu.service.EvaluationService;
import com.nchu.service.OrderService;
import com.nchu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @Autowired
    private EvaluationService evaluationService;

    @RequestMapping("add_order")
    public String orderForm(@RequestParam("type_demand") String type_demand,Model model, HttpSession session){
        System.out.println(type_demand);
        User user = (User) session.getAttribute("user");
        List<UserAddress> addressList = userService.FindAddressbyid(user.getUserId());
        UserAddress userAddress = null;
        try{
            userAddress = addressList.get(0);
        }
        catch (Exception e){
            System.out.println("没有地址");
        }
        model.addAttribute("type_demand",type_demand);
        model.addAttribute("addressList",addressList);
        model.addAttribute("userAddress",userAddress);
        return "add_order";
    }

    @RequestMapping("submit_order")
    public String submitForm(@RequestBody OrderForm orderForm, HttpSession session)
    {
        orderForm.setBillUser((User)session.getAttribute("user"));
        System.out.println(orderForm);
        System.out.println((User) session.getAttribute("user"));
        orderService.addOrder(orderForm);
        return "login";
    }
    @RequestMapping("/submit_order_to_dataBase")
    public String submitOrderToDataBase(@RequestBody Map<String, Object> orderData, HttpSession session){
        int orderID = Integer.parseInt((String) orderData.get("OrderID"));
        User user = (User)session.getAttribute("user");
        int userID = user.getUserId();
        System.out.println("???????????");
        System.out.println(orderID);
        System.out.println(userID);
        System.out.println("???????????");
        orderService.InsertOrderReceivingUserAndChangeOrderStatus(orderID,userID);
        return "login";
    }
    @RequestMapping("display_orders")
    public String displayOrder(Model model){
        List<OrderForm> orderFormList = orderService.queryAllOrderForms();
        model.addAttribute("orderFormList",orderFormList);
        return "run_pool";
    }
    @RequestMapping("display_orders_by_limit")
    @ResponseBody
    public List<OrderForm> displayOrdersByLimit(@RequestBody Filter filter){
        System.out.println(filter);
        filter.setTimeInterval();
        filter.setMoney();
        System.out.println(filter);
        List<OrderForm> orderFormList = orderService.queryOrderFormsByLimit(filter);
        for (OrderForm orderForm : orderFormList) {
            System.out.println(orderForm);
        }
        return orderFormList;
    }
    @RequestMapping("/myrelease")
    public String complain(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyid(userid);
//        formList.stream().forEach(System.out::println);
        model.addAttribute(formList);
        return "order";
    }

    @RequestMapping("/canceled")
    public String canceled(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,0);
        model.addAttribute(formList);
        return "order0";
    }




    @RequestMapping("/obligation")
    public String obligation(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,1);
        model.addAttribute(formList);
        return "order1";
    }
    @RequestMapping("/seize")
    public String seize(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,2);
        model.addAttribute(formList);
        return "order2";
    }
    @RequestMapping("/inprogress")
    public String inprogress(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,3);
        model.addAttribute(formList);
        return "order3";
    }

    @RequestMapping("/completed")
    public String Completed(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,4);
        List<OrderForm> List2 = orderService.FindAllbyidandstatus(userid,5);
        List<OrderForm> List3 = orderService.FindAllbyidandstatus(userid,6);
        List<OrderForm> List4 = orderService.FindAllbyidandstatus(userid,7);
        formList.addAll(List2);
        formList.addAll(List3);
        formList.addAll(List4);
        model.addAttribute(formList);
        return "order4";
    }

    @GetMapping("/complain")
    public String complain(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        OrderForm orderForm = orderService.FindByOrderid(orderid);
        System.out.println(orderForm);
        model.addAttribute("orderForm",orderForm);
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "complaint";
    }


    @GetMapping("/complain0")
    public String complain0(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        OrderForm orderForm = orderService.FindByOrderid(orderid);
        System.out.println(orderForm);
        model.addAttribute("orderForm",orderForm);
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "complaint0";
    }




    @RequestMapping("/Complaintsubmission")
    public String Complaintsubmission(int orderid,int complainantid,int respondentid,String content,Model model, HttpSession session)
    {
//        System.out.println(orderid);
//        System.out.println(complainantid);
//        System.out.println(respondentid);
//        System.out.println(content);
        orderService.AddComplain(orderid,complainantid,respondentid,content);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        orderService.updatacomplainstatus(orderid);
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,4);
        List<OrderForm> List2 = orderService.FindAllbyidandstatus(userid,5);
        List<OrderForm> List3 = orderService.FindAllbyidandstatus(userid,6);
        List<OrderForm> List4 = orderService.FindAllbyidandstatus(userid,7);
        formList.addAll(List2);
        formList.addAll(List3);
        formList.addAll(List4);
        model.addAttribute(formList);
        return "redirect:/order/completed";
    }

    @RequestMapping("/Complaintsubmission0")
    public String Complaintsubmission0(int orderid,int complainantid,int respondentid,String content,Model model, HttpSession session)
    {
//        System.out.println(orderid);
//        System.out.println(complainantid);
//        System.out.println(respondentid);
//        System.out.println(content);
        orderService.AddComplain(orderid,complainantid,respondentid,content);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        orderService.updatacomplainstatus(orderid);
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,4);
        List<OrderForm> List2 = orderService.FindAllbyidandstatus(userid,5);
        List<OrderForm> List3 = orderService.FindAllbyidandstatus(userid,6);
        List<OrderForm> List4 = orderService.FindAllbyidandstatus(userid,7);
        formList.addAll(List2);
        formList.addAll(List3);
        formList.addAll(List4);
        model.addAttribute(formList);
        return "redirect:/order/myrelease";
    }






    @GetMapping("/deleteOrder")
    public String deleteOrder(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        orderService.deletebyorderid(orderid);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyid(userid);
        model.addAttribute(formList);
        return "order";
    }




    @GetMapping("/deleteOrder2")
    public String deleteOrder2(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        orderService.deletebyorderid(orderid);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,2);
        model.addAttribute(formList);
        return "order2";
    }


    @GetMapping("/deleteOrder3")
    public String deleteOrder3(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        orderService.deletebyorderid(orderid);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,3);
        model.addAttribute(formList);
        return "order3";
    }


    @RequestMapping("/myordergrabbing")
    public String myordergrabbing(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindPickUpOrdersByid(userid);
        model.addAttribute(formList);

        return "run_order";
    }
    @GetMapping("/deleteRunorder")
    public String deleteRunorder(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        orderService.deletebyorderid(orderid);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyid(userid);
        model.addAttribute(formList);
        return "redirect:/order/myordergrabbing";
    }


    @GetMapping("/deleteRunorder1")
    public String deleteRunorder2(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        orderService.deletebyorderid(orderid);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyid(userid);
        model.addAttribute(formList);
        return "run_order1";
    }





    @RequestMapping("/Runorderinprogress")
    public String Runorderinprogress(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyreceiveUserandstatus(userid,3);
        model.addAttribute(formList);
        return "run_order1";
    }


    @RequestMapping("/Runordercompleted")
    public String Runordercompleted(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyreceiveUserandstatus(userid,4);
        model.addAttribute(formList);
        return "run_order2";
    }



    @RequestMapping("/Runordercanceled")
    public String Runordercanceled(Model model,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyreceiveUserandstatus(userid,0);
        model.addAttribute(formList);
        return "run_order3";
    }


    @GetMapping("/finishorder")
    public String finishorder(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        orderService.finishorder(orderid);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyid(userid);
        model.addAttribute(formList);
        return "order";
    }

    @GetMapping("/finishorder1")
    public String finishorder1(@RequestParam("orderid") int orderid,Model model,HttpSession session)
    {
        orderService.finishorder(orderid);
        User user = (User) session.getAttribute("user");
        int userid=user.getUserId();
        List<OrderForm> formList = orderService.FindAllbyid(userid);
        model.addAttribute(formList);
        return "redirect:/order/inprogress";
    }


    @RequestMapping("/insertevaluation")
    public String insertevaluation(Model model,HttpSession session,int id,String eva,float evascore)
    {
        OrderForm order = orderService.findone(id);
        OrderEvaluation evaluation=new OrderEvaluation();
        evaluation.setOrderId(id);
        evaluation.setBillUser(order.getBillUser());
        evaluation.setReceiveUser(order.getReceiveUser());
        evaluation.setAppraise(eva);
        evaluation.setBillScore(5);
        evaluation.setReceiveScore(evascore);
        evaluation.setIsFinish(1);
        evaluation.setStatus(0);
        evaluationService.inserteva(evaluation);
        orderService.updataOrderTransationstatus(id);
        return "redirect:/order/completed";
    }

    @RequestMapping("/popoup")
    public String popoup(Model model,int orderid){
        OrderForm findone = orderService.findone(orderid);
        model.addAttribute("ReceiveUser", findone.getReceiveUser());
        return "all";
    }
    @RequestMapping("/inserteva")
    public String inserteva(Model model,int orderid,HttpSession session){
        model.addAttribute("orderid",orderid);
        return "inserteva";
    }

}